<?php

namespace app\bll;

use Exception;
use support\Db;

class UserWidget
{



    /**
     * 查找挂件
     *
     * @param integer $id
     * @param integer $uid
     * @return array
     */
    public function findByUid(int $id, int $uid): array
    {

        $data =  Db::table('frame_user_widget')
            ->where('id', '=', $id)
            ->where('uid', '=', $uid)
            ->first();
        return $data === null ? [] : get_object_vars($data);
    }

    /**
     * 查询某用户挂件
     *
     * @param integer $uid
     * @return array
     */
    public function selectByUid(int $uid): array
    {
        return Db::table('frame_user_widget')
            ->where('uid', '=', $uid)
            ->get()
            ->map(function ($value) {
                return (array)$value;
            })
            ->toArray();
    }

    /**
     * 添加挂件
     *
     * @param integer $uid
     * @param string $widgetame
     * @return integer
     */
    public function add(int $uid, string $widgetame, array $data): int
    {

        $data['uid']    = $uid;
        $data['widget'] = $widgetame;

        try {

            return Db::table('frame_user_widget')
                ->insertGetId($data);
        } catch (Exception $ex) {
            throw $ex;
        }
    }


    /**
     * 编辑
     *
     * @param integer $uid
     * @param string $areaid
     * @param array $data
     * @return integer
     */
    public function edit(int $uid, string $areaid, array $data): int
    {
        try {

            return Db::table('frame_user_widget')
                ->where('uid', '=', $uid)
                ->where('areaid', '=', $areaid)
                ->update($data);
        } catch (Exception $ex) {
            throw $ex;
        }
    }

    /**
     * 删除挂件
     *
     * @param integer $uid
     * @param string $areaid
     * @return integer
     */
    public function delete(int $uid, string $areaid): int
    {
        try {

            return Db::table('frame_user_widget')
                ->where('uid', '=', $uid)
                ->where('areaid', '=', $areaid)
                ->delete();
        } catch (Exception $ex) {
            throw $ex;
        }
    }
}
